package com.etc.service;

import javax.annotation.Resource;

import com.etc.dao.SysOrderMapper;
import com.etc.dao.SysUserMapper;
import com.etc.entity.*;
import com.etc.utils.UUIDTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 订单业务层
 * @author Administrator
 *
 */
@Service
public class SysOrderService {

	private static final Logger log = LoggerFactory.getLogger(SysOrderService.class);
	

	@Resource
	private SysOrderMapper orderMapper;

	@Resource
	private SysUserMapper sysUserMapper;
	
	/**
	 * 添加订单
	 * @param order
	 * @return
	 */
	public Integer add (SysOrder order,Double money){
		Integer relset = null;
		//生成订单id
		order.setOrderNo(UUIDTest.getUUID());
		//添加时间
		order.setOrderDate(String.valueOf(System.currentTimeMillis()));
		//金额
		order.setPayment(String.valueOf(money));
		//1 清晰过程中  2  请洗完成 3 送货中
		order.setStatus("1");
		Integer restle = orderMapper.insert(order);
		if(restle == 1){
			SysUserKey sysUserKey = new SysUserKey();
			sysUserKey.setId(order.getUserId());
			SysUser sysUser = sysUserMapper.selectByPrimaryKey(sysUserKey);
			if(sysUser != null){
				SysUserExample sysUserExample = new SysUserExample();
				SysUserExample.Criteria criteria = sysUserExample.createCriteria();
				criteria.andIdEqualTo(sysUser.getId());
				sysUser.setMony(String.valueOf(Integer.valueOf(sysUser.getMony())-Integer.valueOf(order.getNumber())*10));
				sysUser.setLeiji(String.valueOf(Integer.valueOf(sysUser.getLeiji())+Integer.valueOf(order.getNumber())*10));
				relset = sysUserMapper.updateByExample(sysUser, sysUserExample);
			}
		}
		return relset;
	}

	public Integer fds(Integer id,String status){
		Integer retus = null;
		SysOrderKey sysOrderKey = new SysOrderKey();
		sysOrderKey.setId(id);
		SysOrder sysOrder = orderMapper.selectByPrimaryKey(sysOrderKey);
		if(sysOrder != null ){
			SysOrderExample sysOrderExample = new SysOrderExample();
			SysOrderExample.Criteria criteria = sysOrderExample.createCriteria();
			criteria.andIdEqualTo(id);
			sysOrder.setStatus(status);
			retus  = orderMapper.updateByExample(sysOrder,sysOrderExample);
		}

		return retus;
	}


	/**
	 * 查询没有处理的订单
	 * @return
	 */
	public List<SysOrder> findByweiOrder(){

		SysOrderExample sysOrderExample = new SysOrderExample();
		SysOrderExample.Criteria criteria = sysOrderExample.createCriteria();
		criteria.andStatusEqualTo("1");
		return orderMapper.selectByExample(sysOrderExample);
	}



	public List<SysOrder> findByAll(){
		SysOrderExample sysOrderExample = new SysOrderExample();
		SysOrderExample.Criteria criteria = sysOrderExample.createCriteria();
		criteria.andStatusLessThan("6");
		return orderMapper.selectByExample(sysOrderExample);
	}

	/**
	 * 查询用户的订单
	 * @param id
	 * @return
	 */
	public List<SysOrder> get(String id){
		List<SysOrder> sysOrders = new ArrayList<SysOrder>();
		SysOrderExample sysOrderExample = new SysOrderExample();
		SysOrderExample.Criteria criteria = sysOrderExample.createCriteria();
		criteria.andUserIdEqualTo(Integer.valueOf(id));
		sysOrders = orderMapper.selectByExample(sysOrderExample);
		return sysOrders;
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	public Integer delete(String id){
		Integer restlet = null;
		SysOrderKey staffKey = new SysOrderKey();
		staffKey.setId(Integer.valueOf(id));
		try{
			restlet = orderMapper.deleteByPrimaryKey(staffKey);
		}catch (Exception e) {
			log.error(e.getLocalizedMessage());
		}
		return restlet;
	}




	/**
	 * 
	 * @param id
	 * @return
	 */
	public Integer update(String id){
		Integer restlet = null;
		SysOrderKey staffKey = new SysOrderKey();
		staffKey.setId(Integer.valueOf(id));
		try{
			restlet = orderMapper.deleteByPrimaryKey(staffKey);
		}catch (Exception e) {
			log.error(e.getLocalizedMessage());
		}
		return restlet;
	}




	public SysOrder findById(SysOrderKey key){
		return orderMapper.selectByPrimaryKey(key);
	}

}
